/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.web.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUser;
import com.thinkgem.jeesite.modules.hk.sys.service.user.HkUserService;

/**
 * 用户管理Controller
 * @author 罗天文
 * @version 2018-03-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/user/hkUser")
public class HkUserController extends BaseController {

	@Autowired
	private HkUserService hkUserService;
	
	@ModelAttribute
	public HkUser get(@RequestParam(required=false) String id) {
		HkUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkUserService.get(id);
		}
		if (entity == null){
			entity = new HkUser();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:user:hkUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkUser hkUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkUser> page = hkUserService.findPage(new Page<HkUser>(request, response), hkUser); 
		model.addAttribute("page", page);
		return "hk/sys/user/hkUserList";
	}

	@RequiresPermissions("sys:user:hkUser:view")
	@RequestMapping(value = "form")
	public String form(HkUser hkUser, Model model) {
		model.addAttribute("hkUser", hkUser);
		return "hk/sys/user/hkUserForm";
	}

	@RequiresPermissions("sys:user:hkUser:edit")
	@RequestMapping(value = "save")
	public String save(HkUser hkUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkUser)){
			return form(hkUser, model);
		}
		hkUserService.save(hkUser);
		addMessage(redirectAttributes, "保存用户成功");
		return "redirect:"+Global.getAdminPath()+"/sys/user/hkUser/?repage";
	}
	
	@RequiresPermissions("sys:user:hkUser:edit")
	@RequestMapping(value = "delete")
	public String delete(HkUser hkUser, RedirectAttributes redirectAttributes) {
		hkUserService.delete(hkUser);
		addMessage(redirectAttributes, "删除用户成功");
		return "redirect:"+Global.getAdminPath()+"/sys/user/hkUser/?repage";
	}

}